﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using eCommMongo.Core;
using eCommMongo.Service.Tags;
using eCommMongo.Web.Mapper;

namespace eCommMongo.Web.Controllers.api
{
    public class TagsApiController : ApiController
    {
          private ITagService _tagService;
        private IWorkContext _workContext;

        public TagsApiController(ITagService tagService, IWorkContext workContext)
        {
            _tagService = tagService;
            _workContext = workContext;
        }

        [HttpGet]
        public FindTagsResult FindTags(int pageIndex, int pageSize)
        {
            var result = new FindTagsResult();

            var tags = _tagService.FindTags(pageIndex, pageSize);
            result.rows = tags;
            result.totalRows = tags.TotalCount;
            return result;
        }


        [HttpPost]
        public SaveInterestedTagsResult SaveInterestedTags(SaveInterestedTagsRequest request)
        {
            var result = new SaveInterestedTagsResult();

            try
            {
                var tagIdList = request.TagIdList.Select(c => c.id).ToList();

                _tagService.SaveInterestedTags(_workContext.CurrentCustomer.Id.ToString(), tagIdList);
                result.Status = ConstInfo.ResultSuccessStatus;
            }
            catch (Exception e)
            {
                result.Status = ConstInfo.ResultFailedStatus;
                result.Message = e.Message;
            }

            return result;
        }
    }

    public class SaveInterestedTagsRequest
    {
        public IList<Tag> TagIdList { get; set; }
    }

    public class SaveInterestedTagsResult
    {
        public string Status { get; set; }

        public string Message { get; set; }
    }

    public class FindTagRequest
    {
        public int PageSize { get; set; }
        public int PageIndex { get; set; }
    }

    public class FindTagsResult
    {
        public IPagedList<eCommMongo.Core.Domain.Tags.Tag> rows { get; set; }
        public int totalRows { get; set; }
    }
}
